import random
import itertools
x = ['+', '-', '*', '/']
def random_number():
    y=[]
    for i in range(4):
        y.append(random.randint(1,13))
    return y
def poker(y):
    """
    将生成的随机数以扑克牌的形式输出
    :param y:
    :return:
    """
    y1 = y.copy()
    while 1 in y1 or 11 in y1 or 12 in y1 or 13 in y1:
        if 1 in y1:
            z = y1.index(1)
            y1.remove(1)
            y1.insert(z, 'A')
        if 11 in y1:
            z = y1.index(11)
            y1.remove(11)
            y1.insert(z, 'J')
        if 12 in y1:
            z = y1.index(12)
            y1.remove(12)
            y1.insert(z, 'Q')
        if 13 in y1:
            z = y1.index(13)
            y1.remove(13)
            y1.insert(z, 'K')
        continue
    print("随机抽取的四张牌为：", y1[0], y1[1], y1[2], y1[3])
def judge(y):
    for i in itertools.permutations(y):
        for j in itertools.product(x,repeat=3):
            a=[("{0}{1}{2}{3}{4}{5}{6}".format(i[0], j[0], i[1], j[1], i[2],j[2], i[3])),
               ("(({0}{1}{2}){3}{4}){5}{6}".format(i[0], j[0], i[1], j[1], i[2],j[2], i[3])),
               ("({0}{1}{2}{3}{4}){5}{6}".format(i[0], j[0], i[1], j[1], i[2],j[2], i[3])),
               ("{0}{1}({2}{3}{4}{5}{6})".format(i[0], j[0], i[1], j[1], i[2],j[2], i[3])),
               ("{0}{1}({2}{3}({4}{5}{6}))".format(i[0], j[0], i[1], j[1], i[2],j[2], i[3]))]
            for z in a:
                try:
                    if eval(z)==24:
                        print(z,"=24")
                except:
                    pass
def player():
    y=random_number()
    poker(y)
    expr = input("请输入算式：")
    if eval(expr) == 24:
        print("答案正确，其他答案为：")
        judge(y)
    else:
        print("答案错误，其他答案为：")
        judge(y)
player()

